/**
 * Created by yangxianming on 2020/12/30.
 */
var actions = [""];
//
//接口定义 都是以api开头 所需参数都通过方法传入
//function apiGetCorpList(dofun, envText){}
//

//获取已归档项目列表数据
function apiShowFileList(dofun){
    var xmlHttp = new XMLHttpRequest();
    //year  progressName
    var year = document.getElementById("year").value;
    var progress = document.getElementById("progress");
    var progressValue = selectedText(progress);
    var value = "action=forFile&year="+year+"&progressName="+progressValue;
    xmlHttp.open("get", "/progress/showProgress?" + value, true);
    xmlHttp.onreadystatechange = function(){
        if (xmlHttp.readyState==4){
            if (xmlHttp.status==200){
                var r = JSON.parse(xmlHttp.response);
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                }
                if (r["apiStatus"] == "0"){
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp.send();
}



//

//
//页面渲染 都是以draw开头
//function drawGetCorpList(data){}
//
function drawFileList(data){
    var th = document.getElementById("th");
    var progressList = document.getElementById("progressList");
    progressList.innerText = "";
    progressList.appendChild(th);

    for (var i=0; i<data.length; i++){
        var getDate = data[i][2];
        var yearFull = getDate.split(" ");
        var year = yearFull[0];
        var progressname = data[i][0];
        var isfiledetail = JSON.parse(data[i][1].replace(/[\r\n\s+]/g, '|'));
        var replay = data[i][3];

        var tr = document.createElement("tr");
        var td1 = document.createElement("td");
        td1.innerText = year;
        tr.appendChild(td1);

        var td2 = document.createElement("td");
        td2.innerText = progressname;
        tr.appendChild(td2);

        var td3 = document.createElement("td");
        var isfiledetailStr = "";
        for (var ii=0; ii<isfiledetail["content"].length; ii++){
            var ind = ii + 1;
            isfiledetailStr = isfiledetailStr + ind + "、" + isfiledetail["content"][ii] + "。";
        }
        td3.innerText = "【模块】" + isfiledetail["modules"] + " \n 【具体内容】"  + isfiledetailStr;
        tr.appendChild(td3);

        var td4 = document.createElement("td");
        td4.innerText = isfiledetail["case"];
        tr.appendChild(td4);

        var td5 = document.createElement("td");
        td5.innerText = isfiledetail["bug"];
        tr.appendChild(td5);

        var td6 = document.createElement("td");
        if(replay != ""){
            var a6 = document.createElement("a");
            a6.setAttribute("href", replay);
            a6.setAttribute("target", "_blank");
            a6.innerText = "查看";
            td6.appendChild(a6);
        }
        tr.appendChild(td6);

        //根据replay来确定是否要显示导出，2023年前没有这个数据
        var td7 = document.createElement("td");
        if(replay != ""){
            var replayDoc = replay.replace(/htm/i, "docx");
            var a7 = document.createElement("a");
            a7.setAttribute("href", replayDoc);
            a7.setAttribute("target", "_blank");
            a7.innerText = "导出";
            td7.appendChild(a7);
        }
        tr.appendChild(td7);



        progressList.appendChild(tr);
    }

}

//
//页面调用 数据获取，接口层与渲染层的连接
//


function initializeData(){
    for(var i=1; i<101; i++){
        actions.push(i);
    }
    var th = document.getElementById("th");
    var progressListCont = document.getElementById("progressList");
    progressListCont.innerText = "";
    progressListCont.appendChild(th);

    //查询历史档案页面 填充当前年份
    var da = new Date();
    document.getElementById("year").value = da.getFullYear();

    apiShowFileList(drawFileList);

    //查询历史档案页面 填充项目下拉
    var progressList = ["", "福员外", "口福"];
    loadSelectList(document.getElementById("progress"), progressList)
}